package com.eight.cloud.redisson.utils;

import org.redisson.api.RTopic;
import org.redisson.api.listener.MessageListener;

/**
 * redis监听器
 *
 * @ClassName: RedisListenerActuator
 * @Author: TXC
 * @Date: 2024-10-11 10:14
 **/
public class RedisListenerActuator {

    private final RTopic topic;
    public RedisListenerActuator(RTopic topic) {
        this.topic = topic;
    }

    /**
     * 消息发布
     * @param message 消息
     */
    public final void publish(String message) {
        this.topic.publish(message);
    }

    /**
     * 订阅
     */
    public final void subscribe(MessageListener<Object> listener) {
        this.topic.addListener(Object.class, listener);
    }

    /**
     * 取消订阅
     */
    public final void unsubscribe() {
        this.topic.removeAllListeners();
    }

    /**
     * 取消订阅
     */
    public final void unsubscribe(MessageListener<String> listener) {
        this.topic.removeListener(listener);
    }
}
